﻿Public Class oPromotionItemGet
    Public Function UpdateJsonPromotionItemGet(ByVal lineID As String, ByVal promotionID As String, _
                                  ByVal sourceType As String, ByVal sourceValue As String, ByVal discountMax As Double, _
                                  ByVal getQuantity As Double, ByVal isOptional As Boolean, _
                                  ByVal discountType As String, ByVal discountValue As Double, ByVal getStrType As String, _
                                  ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {promotionID, sourceType, sourceValue, discountMax, getQuantity, isOptional, _
                                           discountType, discountValue, getStrType}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oPromotionItemGet]")
                    query.Append(" ([PromotionID]")
                    query.Append(" ,[SourceType]")
                    query.Append(" ,[SourceValue]")
                    query.Append(" ,[DiscountMax]")
                    query.Append(" ,[GetQuantity]")
                    query.Append(" ,[IsOptional]")
                    query.Append(" ,[DiscountType]")
                    query.Append(" ,[DiscountValue]")
                    query.Append(" ,[GetType])")
                    query.Append("  VALUES(@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7,@Param8,@Param9)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oPromotionItemGet] SET ")
                    query.Append("  [PromotionID] = @Param1")
                    query.Append("  ,[SourceType] = @Param2")
                    query.Append("  ,[SourceValue] = @Param3")
                    query.Append("  ,[DiscountMax] = @Param4")
                    query.Append("  ,[GetQuantity] = @Param5")
                    query.Append("  ,[IsOptional] = @Param6")
                    query.Append("  ,[DiscountType] = @Param7")
                    query.Append("  ,[DiscountValue] = @Param8")
                    query.Append("  ,[GetType] =  @Param9")
                    query.Append(" WHERE [LineID] = @Param10")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {promotionID, sourceType, sourceValue, discountMax, getQuantity, isOptional, _
                                           discountType, discountValue, getStrType, lineID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonPromotionItemGet(ByVal dataBaseName As String, ByVal lineID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {lineID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oPromotionItemGet]")
                query.Append("  WHERE LineID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllPromotionItemGetByPromotionID(ByVal dataBaseName As String, ByVal promotionID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionItemGet")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append("  SELECT * FROM (")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS DiscountTypeName")
                query.Append(" ,T3.ReferenceName AS GetTypeName")
                query.Append(" ,T4.ItemName AS SourceValueName")
                query.Append(" FROM oPromotionItemGet T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.DiscountType = T2.ReferenceValue AND T2.ReferenceCode  =  'DiscountType')")
                query.Append(" LEFT JOIN oReferenceValue T3 ON (T0.GetType = T3.ReferenceValue    AND T3.ReferenceCode  = 'GetType' ) ")
                query.Append(" JOIN oItem T4 ON T4.ItemID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Item Code')")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS DiscountTypeName")
                query.Append(" ,T3.ReferenceName AS GetTypeName")
                query.Append(" ,T4.PropertyDetailName AS SourceValueName")
                query.Append(" FROM oPromotionItemGet T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.DiscountType = T2.ReferenceValue AND T2.ReferenceCode  =  'DiscountType')")
                query.Append(" LEFT JOIN oReferenceValue T3 ON (T0.GetType = T3.ReferenceValue    AND T3.ReferenceCode  = 'GetType' ) ")
                query.Append(" JOIN oItemPropertyDetail T4 ON T4.PropertyDetailID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Item Property' )")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*")
                query.Append(" ,T1.ReferenceName AS ItemSourceTypeName")
                query.Append(" ,T2.ReferenceName AS DiscountTypeName")
                query.Append(" ,T3.ReferenceName AS GetTypeName")
                query.Append(" ,SourceValue AS SourceValueName")
                query.Append(" FROM oPromotionItemGet T0 LEFT JOIN oReferenceValue T1 ON (T0.SourceType = T1.ReferenceValue AND T1.ReferenceCode  = 'ItemSourceType' )")
                query.Append(" LEFT JOIN oReferenceValue T2 ON (T0.DiscountType = T2.ReferenceValue AND T2.ReferenceCode  =  'DiscountType')")
                query.Append(" LEFT JOIN oReferenceValue T3 ON (T0.GetType = T3.ReferenceValue    AND T3.ReferenceCode  = 'GetType' ) ")
                query.Append(" WHERE T1.ReferenceCode = 'ItemSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Query String')")
                query.Append(" ) AS T0")
                query.Append(" ORDER BY LineID DESC")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {promotionID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
